package com.example.coursedesign.ui.notifications;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.coursedesign.R;

import com.example.coursedesign.ui.dashboard.dao.ScienceLikeDao;
import com.example.coursedesign.ui.dashboard.entity.Science;
import com.example.coursedesign.ui.home.dao.MedicineLikeDao;


import com.example.coursedesign.ui.home.entity.Medicine;

import com.example.coursedesign.ui.notifications.adapter.CollectAdapter;

import java.util.List;

public class CollectActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private CollectAdapter collectAdapter;
    private MedicineLikeDao medicineLikeDao;

    private ScienceLikeDao scienceLikeDao;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.collect_nav);

        // 启用返回按钮
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        // 初始化RecyclerView和适配器
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        collectAdapter = new CollectAdapter();
        recyclerView.setAdapter(collectAdapter);

        // 初始化各个Dao
        medicineLikeDao = new MedicineLikeDao((android.app.Application) getApplication());
        scienceLikeDao =new ScienceLikeDao((android.app.Application) getApplication());
        // 设置类别选择器点击事件
        setCategoryClickListener();

        // 默认选中中草药类别
        selectMedicineCategory();
    }

    private void setCategoryClickListener() {
        TextView medicinelikeText = findViewById(R.id.medicinelike);
        medicinelikeText.setOnClickListener(v -> selectMedicineCategory());

        TextView sciencelikeText = findViewById(R.id.sciencelike);
        sciencelikeText.setOnClickListener(v -> selectScienceCategory());

    }
    private void selectMedicineCategory() {
        clearUnderlines();
        View underline = findViewById(R.id.medicinelikeUnderline);
        underline.setVisibility(View.VISIBLE);
        String account = "user123";
        medicineLikeDao.getMedicinesByAccountId(account, new MedicineLikeDao.OnMedicinesFetchedListener() {
            @Override
            public void onMedicinesFetched(List<Medicine> medicines) {
                collectAdapter.setMedicines(medicines);
            }
            @Override
            public void onError(String errorMessage) {
                Toast.makeText(getApplicationContext(), "加载中草药失败: " + errorMessage, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void selectScienceCategory() {
        clearUnderlines();
        View underline = findViewById(R.id.sciencelikeUnderline);
        underline.setVisibility(View.VISIBLE);
        String account = "user123";
        scienceLikeDao.getSciencesByAccountId(account, new ScienceLikeDao.OnSciencesFetchedListener() {
            @Override
            public void onSciencesFetched(List<Science> sciences) {
                collectAdapter.setSciences(sciences);
            }
            @Override
            public void onError(String errorMessage) {
                Toast.makeText(getApplicationContext(), "加载科普失败: " + errorMessage, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void clearUnderlines() {
        findViewById(R.id.medicinelikeUnderline).setVisibility(View.GONE);
        findViewById(R.id.sciencelikeUnderline).setVisibility(View.GONE);
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed(); // 返回上一个 Activity
        return true;
    }
}